$(function(){
	
	$('#goback').click(function(e){
        e.preventDefault();
        window.location.href='/userCenter/uploadInfo';

    })

    $.ajax({
        url:'/bargain/getStep',
        type:'get',
        success:function(data){
        	console.log(data);
            if( data.step == 2 || data.step == 4 || data.step == 6 ){
                $('#shz').addClass('ycActive');
            }else if( data.step == 5 ){
                $('#ytg').addClass('ycActive');
            }else{
 
    $('#section_list').on('click','li div.list_title',function(){

        var height = $(this).siblings('.list_main').css('height');
        var titleHeight = $(this).innerHeight();
        var ycHeight = $(this).siblings('.list_main').innerHeight();

        if( $(this).find('.list_title_arrows').hasClass('active') == false ){

            $(this).parent().animate({
                height:titleHeight+ycHeight
            },300)
            $(this).find('.list_title_arrows').addClass('active');
            $(this).find('.dropDown').css({
                transform:'rotate(90deg)'
            })

        } else{

            $(this).parent().animate({
                height:titleHeight
            },300,function(){
                $(this).find('.list_title_arrows').removeClass('active');
            })
            $(this).find('.dropDown').css({
                transform:'rotate(0deg)'
            })
        }

    })

    //省市联动
    var prev = window.XXarea.pres,
        city = window.XXarea.citys;
    var prevHtml = '',
        cityHtml = '',
        bankArea_cityHTML = '<option value="0">请选择</option>';
    //省
    for( var i=0;i<prev.length;i++ ){
        prevHtml += `
	    	<option value="${i}" data-val='${prev[i]}'>${prev[i]}</option>
    	`
    }
    $('#prev').html(prevHtml);

    $('#bankArea_prev').html(prevHtml);

    //当省发生改变时  修改市的值
    $('#prev').change(function(){
        cityHtml = '';
        var prevVal = $(this).val();
        for( var c=0;c<city[prevVal].length;c++ ){
            cityHtml += `
    	    	<option value="${c}" data-val='${city[prevVal][c]}'>${city[prevVal][c]}</option>
        	`
        }
        $('#city').html(cityHtml);
    })
    //当开户行省发生改变时  修改市的值
    $('#bankArea_prev').change(function(){
        bankArea_cityHTML = '';
        var prevVal = $(this).val();
        for( var c=0;c<city[prevVal].length;c++ ){
            bankArea_cityHTML += `
    	    	<option value="${c}" data-val='${city[prevVal][c]}'>${city[prevVal][c]}</option>
        	`
        }
        $('#bankArea_city').html(bankArea_cityHTML);
    })

    //判断是否添加配偶信息
       $('#pd').click(function(){
            if( $('#maritalStatus').val() == '已婚'){
                $('#spouse_relation_list').removeClass('hideInput');            
                $('#spouse_relation').attr('name','spouse_relation');
                $('#spouse_relation_name').attr('name','spouse');
                $('#spouse_relation_phone').attr('name','spouse_phone');
                                
                $.ajax({
                    url:'/bargain/getJson',
                    type:'get',
                    success:function(data){
	                    var spouse_relationSelectValue = data[4].content[1].content[0].selectValue.split(',');
	                    var html = '';
	                    for(var j=0; j<spouse_relationSelectValue.length;j++){
	                       var selected = ""
	                       if( data[4].content[1].content[0].value !=undefined || data[4].content[1].content[0].value !='' ){
	                           if( spouse_relationSelectValue[j] == data[4].content[1].content[0].value){
	                                selected = "selected='selected'"
	                           }
	                       }
	                       html += `
	                     		<option value="${spouse_relationSelectValue[j]}" ${selected}>${spouse_relationSelectValue[j]}</option>
	                     	`
	                     }
	                    $(`[name='spouse_relation']`).html(html);
                     }
                })
                
             }else{
                 $('#spouse_relation_list').addClass('hideInput');
                                
                 $('#spouse_relation').removeAttr('name');
                 $('#spouse_relation_name').removeAttr('name');
                 $('#spouse_relation_phone').removeAttr('name');
             }
       })


    //获取下拉列表内的数据
    $.ajax({
        url:'/bargain/getJson',
        type:'get',
        success:function(data){
            var dk = data[0].content,
                gz = data[1].content,
                sr = data[2].content,
                zf = data[3].content,
                lxr = data[4].content;

            var paramterName = '';
            var selectValue = '';
            var html = '';
            //贷款信息
            for(var i=0; i<dk.length; i++){
                for(var r=0;r<dk[i].content.length;r++){
                    if(dk[i].content[r].selectValue==undefined || dk[i].content[r].selectValue==''){//说明是txt
                        if( dk[i].content[r].value==undefined || dk[i].content[r].value==''){
                            paramterName = dk[i].content[r].paramterName;
                            $(`input[name=${paramterName}]`).val('');
                        }else{
                            paramterName = dk[i].content[r].paramterName;
                            $(`input[name=${paramterName}]`).val(dk[i].content[r].value);
                            //区域
                            if( paramterName == 'area' ){
                                //console.log( dk[i].content[r].value );
                                var areaValue = dk[i].content[r].value.split(',');
                                var areaPrev = areaValue[0];
                                var areaCity = areaValue[1];

                                //省
                                for( var p=0;p<$('#prev option').length;p++){
                                    if( areaPrev == $('#prev option')[p].innerHTML ){
                                        var that = $('#prev option')[p];
                                        $(that).attr('selected','selected');
                                    }
                                }
                                //市
                                var cityHTML = '';
                                for( var c=0;c<city.length;c++ ){
                                    for( var v=0;v<$('#prev option').length;v++){
                                        if( areaPrev == $('#prev option')[v].innerHTML &&  areaCity == city[$('#prev option')[v].value][c]){
                                            var prevVal = $('#prev option')[v].value;
                                            for( var c=0;c<city[prevVal].length;c++ ){
                                                cityHtml += `
            						    	    	<option value="${c}" data-val='${city[prevVal][c]}'>${city[prevVal][c]}</option>
            						        	`
                                            }
                                            $('#city').html(cityHtml);
                                            for( var t=0;t<$('#city option').length;t++){
                                                if( areaCity == $('#city option')[t].innerHTML ){
                                                    var that = $('#city option')[t];
                                                    $(that).attr('selected','selected');
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            //开户地
                            if( paramterName == 'bankArea' ){

                                var bankAreaValue = dk[i].content[r].value.split(',');
                                var bankAreaPrev = bankAreaValue[0];
                                var bankAreaCity = bankAreaValue[1];

                                for( var bp=0;bp<$('#bankArea_prev option').length;bp++){
                                    if( bankAreaPrev == $('#bankArea_prev option')[bp].innerHTML ){
                                        var that = $('#bankArea_prev option')[bp];
                                        $(that).attr('selected','selected');
                                    }
                                }

                                var bankArea_cityHTML = '';
                                for( var bcc=0;bcc<city.length;bcc++ ){
                                    for( var bv=0;bv<$('#bankArea_prev option').length;bv++){
                                        if( bankAreaPrev == $('#bankArea_prev option')[bv].innerHTML &&  bankAreaCity == city[$('#bankArea_prev option')[bv].value][bcc]){
                                            var bankPrevVal = $('#bankArea_prev option')[bv].value;

                                            for( var bc=0;bc<city[bankPrevVal].length;bc++ ){
                                                bankArea_cityHTML += `
                						    	    	<option value="${bc}" data-val='${city[bankPrevVal][bc]}'>${city[bankPrevVal][bc]}</option>
                						        	`
                                            }
                                            $('#bankArea_city').html(bankArea_cityHTML);

                                            for( var bt=0;bt<$('#bankArea_city option').length;bt++){
                                                if( bankAreaCity == $('#bankArea_city option')[bt].innerHTML ){
                                                    var that = $('#bankArea_city option')[bt];
                                                    $(that).attr('selected','selected');
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }else{//说明是下拉框
                        paramterName = dk[i].content[r].paramterName;
                        selectValue = dk[i].content[r].selectValue.split(',');
                        html = '';
                        for(var j=0; j<selectValue.length;j++){
                            var selected = ""
                            if( dk[i].content[r].value !=undefined || dk[i].content[r].value !='' ){
                                if( selectValue[j] == dk[i].content[r].value){ selected = "selected='selected'" }
                            }
                            html += ` <option value="${selectValue[j]}" ${selected}>${selectValue[j]}</option>	`
                        }
                        $(`[name=${paramterName}]`).html(html);

                        if( paramterName == 'maritalStatus'){
                            //判断是否显示配偶信息
                            console.log( dk[i].content[r].value );
                            if( dk[i].content[r].value == '已婚'){
                                $('#spouse_relation_list').removeClass('hideInput');
                                
                                $('#spouse_relation').attr('name','spouse_relation');
                                $('#spouse_relation_name').attr('name','spouse');
                                $('#spouse_relation_phone').attr('name','spouse_phone');
                            }else{
                            	$('#spouse_relation_list').addClass('hideInput');
 	
                            	$('#spouse_relation').removeAttr('name');
                                $('#spouse_relation_name').removeAttr('name');
                                $('#spouse_relation_phone').removeAttr('name');

                            }
                        }
                    }
                }
            }

            //工作信息
            for(var i=0; i<gz.length; i++){
                for(var r=0;r<gz[i].content.length;r++){
                    if(gz[i].content[r].selectValue==undefined || gz[i].content[r].selectValue==''){//说明是txt
                        
                    	if( gz[i].content[r].value==undefined || gz[i].content[r].value==''){
                            paramterName = gz[i].content[r].paramterName;
                            $(`input[name=${paramterName}]`).val('');
                        }else{
               
                            paramterName = gz[i].content[r].paramterName;
                            if(paramterName == 'position'){
                            	$('#jobInfo').removeClass('hideLi');
                            }
                            $(`input[name=${paramterName}]`).val(gz[i].content[r].value);
                        }
                    }else{//说明是下拉框
                        paramterName = gz[i].content[r].paramterName;
                        selectValue = gz[i].content[r].selectValue.split(',');
                        html = '';
                        for(var j=0; j<selectValue.length;j++){
                            var selected = ""
                            if( gz[i].content[r].value !=undefined || gz[i].content[r].value !='' ){
                                if( selectValue[j] == gz[i].content[r].value){ selected = "selected='selected'" }
                            }
                            html += ` <option value="${selectValue[j]}" ${selected}>${selectValue[j]}</option>	`
                        }
                        $(`[name=${paramterName}]`).html(html);
                    }
                }
            }

            //收入信息
            for(var i=0; i<sr.length; i++){
                for(var r=0;r<sr[i].content.length;r++){
                    if(sr[i].content[r].selectValue==undefined || sr[i].content[r].selectValue==''){//说明是txt
                        if( sr[i].content[r].value==undefined || sr[i].content[r].value==''){
                            paramterName = sr[i].content[r].paramterName;
                            $(`input[name=${paramterName}]`).val('');
                        }else{
                            paramterName = sr[i].content[r].paramterName;
                            $(`input[name=${paramterName}]`).val(sr[i].content[r].value);
                        }
                    }else{//说明是下拉框
                        paramterName = sr[i].content[r].paramterName;
                        selectValue = sr[i].content[r].selectValue.split(',');
                        html = '';
                        for(var j=0; j<selectValue.length;j++){
                            var selected = ""
                            if( sr[i].content[r].value !=undefined || sr[i].content[r].value !='' ){
                                if( selectValue[j] == sr[i].content[r].value){ selected = "selected='selected'"}
                            }
                            html += ` <option value="${selectValue[j]}" ${selected}>${selectValue[j]}</option>	`
                        }
                        $(`[name=${paramterName}]`).html(html);
                    }
                }
            }

            //住房信息
            for(var i=0; i<zf.length; i++){
                for(var r=0;r<zf[i].content.length;r++){
                    if(zf[i].content[r].selectValue==undefined || zf[i].content[r].selectValue==''){//说明是txt
                        if( zf[i].content[r].value==undefined || zf[i].content[r].value==''){
                            paramterName = zf[i].content[r].paramterName;
                            $(`input[name=${paramterName}]`).val('');
                        }else{
                            paramterName = zf[i].content[r].paramterName;
                            $(`input[name=${paramterName}]`).val(zf[i].content[r].value);
                        }
                    }else{//说明是下拉框
                        paramterName = zf[i].content[r].paramterName;
                        selectValue = zf[i].content[r].selectValue.split(',');
                        html = '';
                        for(var j=0; j<selectValue.length;j++){
                            var selected = ""
                            if( zf[i].content[r].value !=undefined || zf[i].content[r].value !='' ){
                                if( selectValue[j] == zf[i].content[r].value){ selected = "selected='selected'" }
                            }
                            html += ` <option value="${selectValue[j]}" ${selected}>${selectValue[j]}</option> `
                        }
                        $(`[name=${paramterName}]`).html(html);
                    }
                }
            }
            //联系人信息
            for(var i=0; i<lxr.length; i++){
                for(var r=0;r<lxr[i].content.length;r++){
                    if(lxr[i].content[r].selectValue==undefined || lxr[i].content[r].selectValue==''){//说明是txt
                        if( lxr[i].content[r].value==undefined || lxr[i].content[r].value==''){
                            paramterName = lxr[i].content[r].paramterName;
                            $(`input[name=${paramterName}]`).val('');
                        }else{
                            paramterName = lxr[i].content[r].paramterName;
                            $(`input[name=${paramterName}]`).val(lxr[i].content[r].value);
                        }
                    }else{//说明是下拉框
                        paramterName = lxr[i].content[r].paramterName;
                        selectValue = lxr[i].content[r].selectValue.split(',');
                        html = '';
                        for(var j=0; j<selectValue.length;j++){
                            var selected = ""
                            if( lxr[i].content[r].value !=undefined || lxr[i].content[r].value !='' ){
                                if( selectValue[j] == lxr[i].content[r].value){ selected = "selected='selected'" }
                            }
                            html += ` <option value="${selectValue[j]}" ${selected}>${selectValue[j]}</option> `
                        }
                        $(`[name=${paramterName}]`).html(html);
                    }
                }
            }



        }

    })
    
    /*
     * 最后提交
     * */
    $('.git').click(function(e){
        e.preventDefault();
        var form = $('[name]:not(meta)');
        var ednum = 0;
        var errDom = null;
        var zk = '';
        for( var ed=0;ed<form.length;ed++){
            if(form[ed].value == '' || form[ed].value == null || form[ed].value == undefined){
                zk = $(form[ed]).parents('.list_main').siblings('.list_title');
                errDom = form[ed];
                break;
            }
            if(form[ed].value != '' && form[ed].value != null && form[ed].value != undefined){
                ednum++;
            }
        }
        if (ednum != form.length) {
            var bodyDom = document.body;
            var titleHeights = zk.innerHeight();
            var ycHeights = zk.siblings('.list_main').innerHeight();
            if( zk.find('.list_title_arrows').hasClass('active') == false ){
                zk.parent().animate({height:titleHeights+ycHeights},300)
                zk.find('.list_title_arrows').addClass('active');
                zk.find('.dropDown').css({transform:'rotate(90deg)'})
            }
            _x.scrollAnimate(bodyDom.scrollTop,$(errDom).offset().top,4,function(pos){
                $(errDom).addClass('error');
                $(errDom).attr('placeholder','不能为空');
                bodyDom.scrollTop = pos
            });
        } else {//不用验证
            $.ajax({
                url:'/bargain/submitFinal',
                type:'get',
                data:$("form").serialize(),
                success:function(data){
                    if(data.code==1){_x.showTip("提交成功！",{url:'/userCenter/uploadInfo'});}
                }
            })
        }
    })
    

    /*
     * 下一页的的按钮
     * */
    $('#section_list').on('click','a.next',function(){
        var openId = $(this).attr('href');
        var open = $(openId).children('.list_title');
        var height = open.siblings('.list_main').css('height');
        var titleHeight = open.innerHeight();
        var ycHeight = open.siblings('.list_main').innerHeight();
        
        

        if( open.find('.list_title_arrows').hasClass('active') == false ){

            open.parent().animate({
                height:titleHeight+ycHeight
            },300)
            open.find('.list_title_arrows').addClass('active');
            open.find('.dropDown').css({
                transform:'rotate(90deg)'
            })

        } else{

            open.parent().animate({
                height:titleHeight
            },300,function(){
                open.find('.list_title_arrows').removeClass('active');
            })
            open.find('.dropDown').css({
                transform:'rotate(0deg)'
            })
        }

        //区域
        var prevVal = $('#prev').val();
        var cityVal = $('#city').val();
        var val = [];
        for( var i=0;i<prev.length;i++){
            if( i == prevVal){
                val.push(prev[i])
            }
        }
        for( var c=0;c<city.length;c++){
            if( c == prevVal ){
                for( var k=0;k<city[c].length;k++){
                    if( k == cityVal ){
                        val.push(city[c][k])
                    }
                }
            }
        }
        $('#prev_all').val( val.join(',') );

        //开户地
        var bankArea_prevVal = $('#bankArea_prev').val();
        var bankArea_cityVal = $('#bankArea_city').val();
        var bankAreaVal = [];
        for( var bi=0;bi<prev.length;bi++){
            if( bi == bankArea_prevVal ){ bankAreaVal.push(prev[bi]) }
        }
        for( var bc=0;bc<city.length;bc++){
            if( bc == bankArea_prevVal ){
                for( var bk=0;bk<city[bc].length;bk++){
                    if( bk == bankArea_cityVal ){
                        bankAreaVal.push(city[bc][bk])
                    }
                }
            }
        }
        $('#bankArea_all').val( bankAreaVal.join(',') );
        $.ajax({
            url:'/bargain/submit',
            type:'get',
            data:$("form").serialize(),
            success:function(data){
                //console.log(data)
            	}
        })
    })
    
    /*
     * 正则判断
     * */
    var numReg = /^[0-9]*$/;
    var ybReg = /[1-9]\d{5}(?!\d)/;
    var phoneNumReg = /^1[3|4|5|7|8][0-9]{9}$/;
    var PhoneReg = /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/;

    $('#section_list').on('focus','[type=number]',function(){
        $(this).val('');
        $(this).removeClass('error');
        $(this).attr('placeholder','');
    })
    $('#section_list').on('blur','[type=number]',function(){
        if ($(this).val() == '') {
            $(this).addClass('error');
            $(this).attr('placeholder','不能为空');

        }
        else {
            if (numReg.test($(this).val()) == false) {
                $(this).addClass('error');
                $(this).val('');
                $(this).attr('placeholder','格式错误');
            }
        }
    })

    $('#section_list').on('focus','[data-reg="phone"]',function(){
        $(this).val('');
        $(this).removeClass('error');
        $(this).attr('placeholder','');
    })
    $('#section_list').on('blur','[data-reg="phone"]',function(){
        if ($(this).val() == '') {
            $(this).addClass('error');
            $(this).attr('placeholder','不能为空');
        }
        else {
            if (phoneNumReg.test($(this).val()) == false) {
                $(this).addClass('error');
                $(this).val('');
                $(this).attr('placeholder','格式错误');
            }
        }
    })

    $('#section_list').on('focus','[data-reg=yb]',function(){
        $(this).val('');
        $(this).removeClass('error');
        $(this).attr('placeholder','');
    })
    $('#section_list').on('blur','[data-reg=yb]',function(){
        if ($(this).val() == '') {
            $(this).addClass('error');
            $(this).attr('placeholder','不能为空');
        }
        else {
            if (ybReg.test($(this).val()) == false) {
                $(this).addClass('error');
                $(this).val('');
                $(this).attr('placeholder','格式错误');
            }
        }
    })

/*
     * 添加法人下面的两栏信息
     * */
    $('input[name=position]').blur(function(){
    	var positionReg = new RegExp('法人', 'g')
        var positionVal = $(this).val();
    	if(positionReg.test(positionVal) == true){		
    		//$('#section_list li.hideLi').show();
    		$('#section_list li.hideLi').removeClass('hideLi');
            $('#workInfoList').siblings('a').attr('href','#jobInfo');
            
    		
    		//console.log($('#workInfoList').find('li.hideLi'));
    		/*var positionSiblingsHTML = `
    		<li>
	            <p class="list_main_data_list_title list_main_data_text">股权占比</p>
	            <div class="list_main_data_form">
	                <input type="text" class="select_border input_padding" name='stock'>
	            </div>
	        </li>
	        <li>
	            <p class="list_main_data_list_title list_main_data_text">员工人数</p>
	            <div class="list_main_data_form">
	                <input type="text" class="select_border input_padding" name='workersCount'>
	            </div>
	        </li>
    		`
    		$('#workInfoList').append(positionSiblingsHTML);
    		var innerHTMLHeight = $('#workInfo').find('.list_main').innerHeight();
    		$('#workInfo .list_main').css({height:innerHTMLHeight});*/
    		/*var titleHeight = $(this).parent('.list_main').siblings('.list_title').innerHeight();
            var ycHeight = $(this).parent('.list_main').innerHeight();
            console.log(titleHeight);
            console.log(ycHeight);
            console.log( $('#workInfo').find('.list_main').innerHeight() );
            $('#workInfo .list_main').css({
            	height:$('#workInfo').find('.list_main').innerHeight()
            })
                
            console.log( $('#workInfo .list_main').innerHeight() );
            $('#workInfo .list_main').height($('#workInfo .list_main').innerHeight());*/
    		
    	}
    })






            }
        }
    })
    
    
    
    
    
    
   

});













